package com.sky.generate;

import com.sky.model.Contract;
import com.sky.model.Resource;

import java.io.*;

/**
 * @author zhijun.guo
 * @create 2019-02-06 15:59
 **/
public class GenerateFactory {
    public final static String ENTER = System.getProperty("line.separator");

    private final static String T_SEARCH_PATH = "E:\\settlement\\webapi\\common\\template";

    private final static String T_JS_FILE = "Template.js";
    private final static String T_HTML_FILE = "Template.html";


    private final static String WEB_ROOT = "E:\\settlement\\webapp";

    private final static String ACTION_ROOT = "E:\\settlement\\src\\main\\com\\sky\\action";

    private final static String SERVICE_ROOT = "E:\\settlement\\src\\main\\com\\sky\\service";
    private final static String MAPPER_ROOT = "E:\\settlement\\src\\main\\com\\sky\\dao";

    private final static String T_ACTION_FILE = "TemplateAction.java";

    private final static String T_SERVICE_FILE = "TemplateService.java";

    private final static String T_MAPPER_FILE = "TemplateMapper.java";

    private Class clazz;

    public GenerateFactory(Class clazz){
        this.clazz = clazz;
    }

    public static void main(String[] args) throws Exception {
        Class clazz = Contract.class;
        GenerateFactory factory = new GenerateFactory(clazz);
        factory.buildSaarchTemplate();
    }

    public void buildSaarchTemplate() throws Exception {
        //TODO:读取html js 模板
        GenerateTemplate generateTemplate = new GenerateTemplate();

        String oHTMLResult = generateTemplate.replaceHTMLContent(readTemplate(T_HTML_FILE), this.clazz);

        this.writeTemplate(oHTMLResult ,this.clazz.getSimpleName() + "Search.html");

        String oJSResult = generateTemplate.replacejJSContent(readTemplate(T_JS_FILE), this.clazz);
        this.writeTemplate(oJSResult ,this.clazz.getSimpleName() + "Search.js");

        String oACTIONResult = generateTemplate.replacejClassContent(readTemplate(T_ACTION_FILE), this.clazz);
        this.writeTemplate(ACTION_ROOT,oACTIONResult ,this.clazz.getSimpleName() + "Action.java");

        String oSERVICEResult = generateTemplate.replacejClassContent(readTemplate(T_SERVICE_FILE), this.clazz);
        this.writeTemplate(SERVICE_ROOT,oSERVICEResult ,this.clazz.getSimpleName() + "Service.java");

        String oMAPPERResult = generateTemplate.replacejClassContent(readTemplate(T_MAPPER_FILE), this.clazz);
        this.writeTemplate(MAPPER_ROOT,oMAPPERResult ,this.clazz.getSimpleName() + "Mapper.java");
    }

    public void writeTemplate(String root,String content,String name) {
        BufferedWriter bw = null;
        try {
            File dir=new File(root);
            if(!dir.exists()) dir.mkdir();

            File file = new File(root + "\\" + name);
            if(!file.exists()) file.createNewFile();

            bw = new BufferedWriter(new FileWriter(file));
            bw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void writeTemplate(String content,String name) {
        String pathdir = WEB_ROOT + "\\" + this.clazz.getSimpleName();
        BufferedWriter bw = null;
        try {
            File dir=new File(pathdir);
            if(!dir.exists()) dir.mkdir();

            File file = new File(pathdir + "\\" + name);
            if(!file.exists()) file.createNewFile();

            bw = new BufferedWriter(new FileWriter(file));
            bw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private static String readTemplate(String name) throws Exception {
        if(name.equals("") || name == null) throw new Exception("must input key");
        StringBuffer tmeplate = new StringBuffer();
        FileInputStream fis = null;
        BufferedReader br = null;
        try {
            String tempStr = "";
            fis = new FileInputStream(new File(T_SEARCH_PATH + "\\" + name));
            br = new BufferedReader(new InputStreamReader(fis));
            while ((tempStr = br.readLine()) != null){
                tmeplate.append(tempStr+ENTER);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                fis.close();
            }catch (Exception e1){
                e1.printStackTrace();
            }
        }
        return tmeplate.toString();
    }
}